//
//  MainViewController.swift
//  zhihuifan
//
//  Created by apple on 2017/8/10.
//  Copyright © 2017年 youyou. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //设置子控制器
        setupChildViewControllers()
       // self.tabBar.backgroundColor = UIColor.red
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

 fileprivate extension MainViewController{
    func setupChildViewControllers(){
       //取出json数据路径
     guard let path = Bundle.main.path(forResource: "main.json", ofType: nil),
        let data = NSData(contentsOfFile: path) ,
        let json = try? JSONSerialization.jsonObject(with: data as Data, options: []) as? [String:Any],
        let array:Array = json?["array"] as? [[String:String]]
        else{
            print("失败")
            return
        }
        var arrayControllers:[UIViewController]? = [UIViewController]()
        for dic in array {
       let vc = setupController(dic: dic)
            arrayControllers?.append(vc)
        }
        self.viewControllers = arrayControllers
        //self.tabBar.tintColor = #colorLiteral(red: 0.3176470588, green: 0.631372549, blue: 1, alpha: 1)


    }
    func setupController(dic:[String:Any]) -> UIViewController{
        let namespace = Bundle.main.nameSpace()
        //取得字典内容
      guard  let clsName = dic["viewController"] as? String,
             let tabbarImage = dic["image"] as? String,
             let selectImage = dic["select_image"] as? String,
        let title = dic["title"] as? String,
        let cls = NSClassFromString(namespace + "." + clsName) as? ZHFBaseViewController.Type,
        let navTitle = dic["navTitle"] as? String
        else {
            return UIViewController()
        }
        //创建控制器
        let vc = cls.init()
        vc.title = navTitle
        vc.tabBarItem.title = title
        vc.tabBarItem.image = UIImage(named: tabbarImage)
        vc.tabBarItem.selectedImage = UIImage(named: selectImage)
        let navVC = ZHFNavgationController(rootViewController: vc)
        if clsName == "HomepageViewController" {
            navVC.navigationBar.isHidden = true

        }
        return navVC
    }
}
